package com.chushouya.order.service.admin.impl;

import com.general.framework.core.lang.Dates;
import com.chushouya.order.dao.entity.StatManageDayEntity;
import com.chushouya.order.dao.repository.StatManageDayRepository;
import com.chushouya.order.dto.admin.stat.ManageDataDTO;
import com.chushouya.order.dto.admin.stat.ManageDayStatQuery;
import com.chushouya.order.service.admin.ManageStatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 运营统计数据服务实现
 */
@Slf4j
@Service
public class ManageStatServiceImpl implements ManageStatService {

    @Resource
    private StatManageDayRepository statManageDayRepository;

    @Override
    public List<ManageDataDTO> getMonthlyStatData() {
        Date today = Dates.getFirstTime(new Date());
        Date startDate = Dates.getFirstTime(Dates.addMonths(today, -1));
        
        return getStatDataByDateRange(startDate, today);
    }

    @Override
    public List<ManageDataDTO> getStatDataByDateRange(Date startDate, Date endDate) {
        log.info("查询统计数据，日期范围：{} 到 {}", startDate, endDate);
        
        ManageDayStatQuery query = new ManageDayStatQuery();
        query.setStatDateStart(startDate);
        query.setStatDateEnd(endDate);
        
        List<StatManageDayEntity> entities = statManageDayRepository.selectList(query);
        List<ManageDataDTO> result = new ArrayList<>();
        
        for (StatManageDayEntity entity : entities) {
            ManageDataDTO dto = new ManageDataDTO();
            dto.setStatDate(entity.getStatDate());
            dto.setOrderCount(entity.getOrderCount());
            dto.setUserCount(entity.getUserCount());
            dto.setVisitCount(entity.getVisitCount());
            dto.setEvaluateCount(entity.getEvaluateCount());
            dto.setProductCount(entity.getProductCount());
            result.add(dto);
        }
        
        log.info("查询到 {} 条统计数据", result.size());
        return result;
    }

    @Override
    public ManageDataDTO getMonthlySummaryData() {
        Date today = Dates.getFirstTime(new Date());
        Date startDate = Dates.getFirstTime(Dates.addMonths(today, -1));
        
        log.info("查询一个月汇总统计数据，日期范围：{} 到 {}", startDate, today);
        
        // 使用Repository中的汇总查询方法
        ManageDataDTO summaryData = statManageDayRepository.selectManageCount(startDate, today);
        
        if (summaryData == null) {
            log.warn("未查询到汇总统计数据，返回空数据");
            summaryData = new ManageDataDTO();
            summaryData.setOrderCount(0);
            summaryData.setUserCount(0);
            summaryData.setVisitCount(0);
            summaryData.setEvaluateCount(0);
            summaryData.setProductCount(0);
        }
        
        log.info("一个月汇总数据：订单数={}, 用户数={}, 访问数={}, 估价数={}, 产品数={}", 
                summaryData.getOrderCount(), summaryData.getUserCount(), 
                summaryData.getVisitCount(), summaryData.getEvaluateCount(), 
                summaryData.getProductCount());
        
        return summaryData;
    }
}
